
package myfx.component;

import Globals.AllGlobal;
import brain.MyBrain;
import controller.MyController;
import database.operate.OperateMyDatabaseImpl;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import model.ZuanChang_Model;

/**
 *
 * @author 周明瑞
 */
public class ZuanChang_Copy extends Stage{
    //
    private MyController controller;
    private ZuanChang_Model zuanChang_Model;
    private ZuanChang_Model zc_info;
    private String zuanChangName;
    private MyBrain brain;
    TextFieldTreeCellImpl textFieldTreeCellImpl;
    private Scene scene ;
    private Group root=new Group();
    int drawType=AllGlobal.DRAW_ONE;
    final TextField mingchengField=new TextField();
    final TextField shenduField=new TextField();
    final TextField cjrqField=new TextField();
    final TextField gzmField=new TextField();
    final TextField xdzbXField=new TextField();
    final TextField xdzbYField=new TextField();
    final TextField kxlField=new TextField();
    final TextField yzxField=new TextField();
    public ZuanChang_Copy
            (MyController controller,String zuanChangName,TextFieldTreeCellImpl textFieldTreeCellImpl,MyBrain brain,
            Stage owner, boolean modality, String title)
            throws Exception{
        super();
        initOwner(owner);
        Modality m = modality ? Modality.APPLICATION_MODAL : Modality.NONE;
        initModality(m);
        setTitle(title);
        scene=new Scene(root, 500, 380, Color.WHITE);
        setScene(scene);
        this.controller=controller;
        this.brain=brain;
        this.zuanChangName=zuanChangName;
        zuanChang_Model=new ZuanChang_Model();
        this.controller=controller;
        this.textFieldTreeCellImpl=textFieldTreeCellImpl;
        this.brain=brain;
        this.setResizable(false);
        
        MyCard mc1=new MyCard(490, 265, "量参调节",Color.DARKGRAY);
        mc1.setTranslateX(5);
        mc1.setTranslateY(5);
        root.getChildren().add(mc1);
        MyCard mc2=new MyCard(490, 30, "输入处理",Color.DARKGRAY);
        mc2.setTranslateX(5);
        mc2.setTranslateY(295);
        root.getChildren().add(mc2);
        
        createScene();
        ArrayList<ZuanChang_Model> zcList;
        if((zcList=OperateMyDatabaseImpl.getOne(AllGlobal.ZUANCHANG_SQL, 
            "select * from zuanchang where mingcheng='"+textFieldTreeCellImpl.getTreeItem().getValue()+"';"
           )).isEmpty()){
            //controller.getJmeCanvas().setZuanKong(zuanKong_Model,AllGlobal.DRAW_ONE);
            zc_info=new ZuanChang_Model();
            
        } 
        else{
            
            zc_info=zcList.get(0);
            drawType=AllGlobal.UPDATE_ONE;
            mingchengField.setText(zc_info.getMingcheng());
            shenduField.setText(zc_info.getShendu());
            
            gzmField.setText(zc_info.getGongzuomian());
            xdzbXField.setText(zc_info.getXdzbx());
            xdzbYField.setText(zc_info.getXdzby());
            kxlField.setText(zc_info.getKongxilvString());
            yzxField.setText(zc_info.getYanzhanxing());
        }       
    }
    private void createScene(){
        //信息标示
        final Text warningText=new Text("当前所处状态");
        warningText.setFill(Color.GREEN);
        warningText.setFont(new Font(18));
        warningText.setTranslateX(15);
        warningText.setTranslateY(365);
        root.getChildren().add(warningText);
        
        Text mcText=new Text("钻场名称");
        
        mingchengField.setText("");
        mingchengField.setPromptText("请输入钻场名称");
        mingchengField.setPrefSize(150, 12);
        mingchengField.setTranslateX(80);
        mingchengField.setTranslateY(40);
        mcText.setTranslateX(15);
        mcText.setTranslateY(55);
        root.getChildren().add(mingchengField);
        root.getChildren().add(mcText);
        Text sdText=new Text("钻场深度");
        
        shenduField.setPromptText("请输入钻场深度");
        shenduField.setPrefSize(150, 12);
        shenduField.setTranslateX(315);
        shenduField.setTranslateY(40);
        sdText.setTranslateX(250);
        sdText.setTranslateY(55);
        root.getChildren().add(shenduField);
        root.getChildren().add(sdText);
        Text cjrqText=new Text("创建日期");
        
        cjrqField.setText("2015年1月22日");
        cjrqField.setVisible(false);
        cjrqField.setPrefSize(150, 12);
        cjrqField.setTranslateX(80);
        cjrqField.setTranslateY(63);
        cjrqText.setTranslateX(15);
        cjrqText.setTranslateY(78);
        cjrqField.setEditable(false);
        root.getChildren().add(cjrqField);
        root.getChildren().add(cjrqText);
        Text gzmmcText=new Text("工作面名称");
        
        gzmField.setText("");
        gzmField.setPrefSize(150, 12);
        gzmField.setTranslateX(315);
        gzmField.setTranslateY(63);
        gzmmcText.setTranslateX(250);
        gzmmcText.setTranslateY(78);
        gzmField.setEditable(false);
        root.getChildren().add(gzmField);
        root.getChildren().add(gzmmcText);
        Text xxzyText=new Text("注意：钻场的相对坐标是笛卡尔坐标系，以工作面基点为原点，以顺槽为X轴且向右为正，\nY轴向上为正，单位为m");
        xxzyText.setFill(Color.RED);
        xxzyText.setTranslateX(15);
        xxzyText.setTranslateY(101);
        root.getChildren().add(xxzyText);
        Text xdzbxText=new Text("相对坐标X");
        
        xdzbXField.setPromptText("请输入笛卡尔相对坐标X");
        xdzbXField.setPrefSize(150, 12);
        xdzbXField.setTranslateX(80);
        xdzbXField.setTranslateY(125);
        xdzbxText.setTranslateX(15);
        xdzbxText.setTranslateY(140);
        root.getChildren().add(xdzbXField);
        root.getChildren().add(xdzbxText);
        Text xdzbyText=new Text("相对坐标Y");
        
        xdzbYField.setPromptText("请输入笛卡尔相对坐标Y");
        xdzbYField.setPrefSize(150, 12);
        xdzbYField.setTranslateX(315);
        xdzbYField.setTranslateY(125);
        xdzbyText.setTranslateX(250);
        xdzbyText.setTranslateY(140);
        root.getChildren().add(xdzbYField);
        root.getChildren().add(xdzbyText);
        Text kxlText=new Text("孔隙率");
        
        kxlField.setText("0.4");
        kxlField.setPrefSize(150, 12);
        kxlField.setTranslateX(80);
        kxlField.setTranslateY(148);
        kxlText.setTranslateX(15);
        kxlText.setTranslateY(163);
        kxlField.setEditable(false);
        root.getChildren().add(kxlField);
        root.getChildren().add(kxlText);
        Text yzxText=new Text("延展性");
        
        yzxField.setText("1");
        yzxField.setPrefSize(150, 12);
        yzxField.setTranslateX(315);
        yzxField.setTranslateY(148);
        yzxText.setTranslateX(250);
        yzxText.setTranslateY(163);
        root.getChildren().add(yzxField);
        root.getChildren().add(yzxText);
        Button ok=new Button("确      认");
        Button retry=new Button("重新输入");
        ok.setTranslateX(170);
        ok.setTranslateY(322);       
        retry.setTranslateX(250);
        retry.setTranslateY(322);
        root.getChildren().add(retry);
        root.getChildren().add(ok);
        //定义事件行为
        ok.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                try {
                    ArrayList<ZuanChang_Model> zcList;
                    if((zcList=OperateMyDatabaseImpl.getOne(AllGlobal.ZUANCHANG_SQL, 
                        "select * from zuanchang where mingcheng='"+textFieldTreeCellImpl.getTreeItem().getValue()+"';"
                       )).isEmpty()){
                        drawType=AllGlobal.DRAW_ONE;
                    } 
                    else{
                        drawType=AllGlobal.UPDATE_ONE;
                    }      
                    zuanChang_Model.setMingcheng(mingchengField.getText());
                    zuanChang_Model.setShendu(shenduField.getText());
                    zuanChang_Model.setXdzbx(xdzbXField.getText());  
                    zuanChang_Model.setXdzby(xdzbYField.getText()); 
                    zuanChang_Model.setGongzuomian(gzmField.getText());
                    zuanChang_Model.setYanzhanxing(yzxField.getText());
                    zuanChang_Model.setKongxilvString(kxlField.getText());   
                    if(drawType==AllGlobal.DRAW_ONE)
                        //数据库处理
                        OperateMyDatabaseImpl.doAdd("insert into zuanchang values ('"+
                                zuanChang_Model.getMingcheng()+"','"+
                                zuanChang_Model.getShendu()+"','"+
                                zuanChang_Model.getXdzbx()+"','"+
                                zuanChang_Model.getXdzby()+"','"+
                                zuanChang_Model.getGongzuomian()+"','"+
                                zuanChang_Model.getYanzhanxing()+"','"+
                                zuanChang_Model.getKongxilvString()+
                                "');");
                    else{
                        OperateMyDatabaseImpl.doUpdate("update zuanchang set "+
                                "mingcheng='"+zuanChang_Model.getMingcheng()+"',"+
                                "shendu='"+zuanChang_Model.getShendu()+"',"+
                                "xdzbx='"+zuanChang_Model.getXdzbx()+"',"+
                                "xdzby='"+zuanChang_Model.getXdzby()+"',"+
                                "gongzuomian='"+zuanChang_Model.getGongzuomian()+"',"+
                                "yanzhanxing='"+zuanChang_Model.getYanzhanxing()+"',"+
                                "kongxilv='"+zuanChang_Model.getKongxilvString()+"'"+
                                " where mingcheng='"+textFieldTreeCellImpl.getTreeItem().getValue()+"';");
                    }
                    textFieldTreeCellImpl.getTreeItem().setValue(mingchengField.getText());
                    warningText.setText("成功修改数据");
                } catch (Exception ex) {
                    Logger.getLogger(ZuanChang_Copy.class.getName()).log(Level.SEVERE, null, ex);
                    warningText.setText("数据修改失败");
                }
            }
        });
        retry.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                mingchengField.setText(zc_info.getMingcheng());
                shenduField.setText(zc_info.getShendu());

                gzmField.setText(zc_info.getGongzuomian());
                xdzbXField.setText(zc_info.getXdzbx());
                xdzbYField.setText(zc_info.getXdzby());
                kxlField.setText(zc_info.getKongxilvString());
                yzxField.setText(zc_info.getYanzhanxing());
                warningText.setText("重新进行数据输入");
            }
        });
    }
}

